#!/bin/bash
set -e
cd "$(dirname "$0")"

TERMUX_PKG_NAME="termux-x11-nightly"
TERMUX_PKG_HOMEPAGE="https://github.com/termux/termux-x11"
TERMUX_PKG_DESCRIPTION="Companion package for termux-x11 app"
TERMUX_PKG_MAINTAINER="Twaik Yont @twaik"
TERMUX_PKG_REVISION=0
TERMUX_PKG_VERSION=$(grep "version =" app/build.gradle | cut -d '"' -f 2)
TERMUX_PKG_DEPENDS="xkeyboard-config"

TERMUX_PKG_FULLVERSION="$TERMUX_PKG_VERSION-$TERMUX_PKG_REVISION"

### Deploy .deb package

DEB_PACKAGE_PATH=app/build/outputs/apk/debug/${TERMUX_PKG_NAME}-${TERMUX_PKG_FULLVERSION}-all.deb
PACMAN_PACKAGE_PATH=app/build/outputs/apk/debug/${TERMUX_PKG_NAME}-${TERMUX_PKG_FULLVERSION}-any.pkg.tar.xz

INTERMEDIATES=shell-loader/build/intermediates
DATA_DIR=$INTERMEDIATES/data
CONTROL_DIR=$INTERMEDIATES/control
PACKAGE_DIR=$INTERMEDIATES/package
PREFIX=$DATA_DIR/data/data/com.termux/files/usr

rm -rf $DEB_PACKAGE_PATH $PACMAN_PACKAGE_PATH $DATA_DIR $CONTROL_DIR $PACKAGE_DIR $INTERMEDIATES/.PKGINFO $INTERMEDIATES/.BUILDINFO $INTERMEDIATES/.MTREE

mkdir -p $PREFIX/bin/
mkdir -p $PREFIX/libexec/termux-x11
mkdir -p "$(dirname $DEB_PACKAGE_PATH)"

cp termux-x11 $PREFIX/bin/
cp termux-x11-preference $PREFIX/bin/
cp shell-loader/build/outputs/apk/debug/shell-loader-debug.apk \
	$PREFIX/libexec/termux-x11/loader.apk

mkdir -p $CONTROL_DIR
cat <<EOF > $CONTROL_DIR/control
Package: $TERMUX_PKG_NAME
Architecture: all
Maintainer: $TERMUX_PKG_MAINTAINER
Version: $TERMUX_PKG_FULLVERSION
Homepage: $TERMUX_PKG_HOMEPAGE
Depends: $TERMUX_PKG_DEPENDS
Description: $TERMUX_PKG_DESCRIPTION
EOF

cat <<EOF > $CONTROL_DIR/postinst
#!/data/data/com.termux/files/usr/bin/sh
chmod 400 /data/data/com.termux/files/usr/libexec/termux-x11/loader.apk
EOF

mkdir -p $PACKAGE_DIR
echo 2.0 > $PACKAGE_DIR/debian-binary
tar -cJf $PACKAGE_DIR/data.tar.xz -C $DATA_DIR .
tar -czf $PACKAGE_DIR/control.tar.gz -C $CONTROL_DIR .

ar -rsc $DEB_PACKAGE_PATH \
  $PACKAGE_DIR/debian-binary \
  $PACKAGE_DIR/control.tar.gz \
  $PACKAGE_DIR/data.tar.xz

### Deploy pacman package

BUILD_DATE=$(date +%s)
TERMUX_PKG_INSTALLSIZE="$(du -bs $DATA_DIR | cut -f 1)"

{
  echo "pkgname = $TERMUX_PKG_NAME"
  echo "pkgbase = $TERMUX_PKG_NAME"
  echo "pkgver = $TERMUX_PKG_FULLVERSION"
  echo "pkgdesc = $(echo "$TERMUX_PKG_DESCRIPTION" | tr '\n' ' ')"
  echo "url = $TERMUX_PKG_HOMEPAGE"
  echo "builddate = $BUILD_DATE"
  echo "packager = $TERMUX_PKG_MAINTAINER"
  echo "size = $TERMUX_PKG_INSTALLSIZE"
  echo "arch = any"
  echo "license = GPL-3.0"
  echo "replaces = termux-x11"
  echo "conflict = termux-x11"
  echo "provides = termux-x11"
  tr ',' '\n' <<< "$TERMUX_PKG_DEPENDS" | sed 's|(||g; s|)||g; s| ||g; s|>>|>|g; s|<<|<|g' | awk '{ printf "depend = " $1; if ( ($1 ~ /</ || $1 ~ />/ || $1 ~ /=/) && $1 !~ /-/ ) printf "-0"; printf "\n" }' | sed 's/|.*//'
} > $DATA_DIR/.PKGINFO

{
  echo "format = 2"
  echo "pkgname = $TERMUX_PKG_NAME"
  echo "pkgbase = $TERMUX_PKG_NAME"
  echo "pkgver = $TERMUX_PKG_FULLVERSION"
  echo "pkgarch = any"
  echo "packager = $TERMUX_PKG_MAINTAINER"
  echo "builddate = $BUILD_DATE"
} > $DATA_DIR/.BUILDINFO

{
  echo "post_install() {"
  echo "    chmod 400 /data/data/com.termux/files/usr/libexec/termux-x11/loader.apk"
  echo "}"
  echo "post_upgrade() {"
  echo "    post_install"
  echo "}"
} > $DATA_DIR/.INSTALL

PACMAN_PACKAGE_PATH=`realpath $PACMAN_PACKAGE_PATH`

cd $DATA_DIR
shopt -s dotglob globstar
mapfile -d '' FILES < <(printf '%s\0' .BUILDINFO .INSTALL .PKGINFO data/**/*)
shopt -u dotglob globstar

# Bsdtar is missing in Github CI image and installing it takes a while
# so effectively this code replaces
# printf '%s\0' data/**/* .BUILDINFO .INSTALL .PKGINFO | bsdtar -cnf - --format=mtree --options='!all,use-set,type,uid,gid,mode,time,size,md5,sha256,link' --null --files-from - | gzip -c -f -n > .MTREE
# printf '%s\0' data/**/* .BUILDINFO .INSTALL .PKGINFO .MTREE | bsdtar --no-fflags -cnf - --null --files-from - | xz > "$PACMAN_PACKAGE_PATH"

{
_mode="664"
echo "#mtree"
echo "/set type=file uid=1000 gid=1000 mode=$_mode"
for path in "${FILES[@]}"; do
  # Skip non-existing paths (due to globs resolving to themselves)
  [ -e "$path" ] || continue

  if [ -d "$path" ]; then
    mode="$(stat -c '%a' "$path")"
    mtime="$(stat -c '%Y' "$path").$(stat -c '%y' "$path" | cut -d'.' -f2 | cut -d' ' -f1)"
    if [ "$mode" != "$_mode" ]; then
        echo "/set mode=$mode"
        _mode="$mode"
    fi
    printf '%q time=%s type=dir\n' "$path" "$mtime"

  elif [ -f "$path" ]; then
    mode="$(stat -c '%a' "$path")"
    mtime="$(stat -c '%Y' "$path").$(stat -c '%y' "$path" | cut -d'.' -f2 | cut -d' ' -f1)"
    size="$(stat -c '%s' "$path")"
    md5="$(md5sum "$path" | cut -d' ' -f1)"
    sha256="$(sha256sum "$path" | cut -d' ' -f1)"
    printf '%q time=%s%s size=%s md5digest=%s sha256digest=%s%s\n' \
      "$path" "$mtime" "$([ "$mode" != "$_mode" ] && echo " mode=$mode")" "$size" "$md5" "$sha256"
  fi
done
} | gzip > .MTREE

printf '%s\0' "${FILES[@]}" ".MTREE" | tar --null --no-recursion --hard-dereference --files-from=- -cf - | xz > "$PACMAN_PACKAGE_PATH"
